package com.surface.hao.frame;

import com.surface.hao.component.ScrollPanel;
import com.surface.hao.util.JsonUtils;
import lombok.extern.log4j.Log4j;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description
 *  json字符串格式化Panel
 * @Date 2020/1/1
 * @Author songhao
 **/
@Log4j
public class JsonFormatPanel {

    static ScrollPanel inputArea = null;
    static ScrollPanel tipArea = null;

    public static JPanel getJpanel(String titleName, JFrame jFrame) {
        JPanel panel = new JPanel();
        //设置布局方式和大小
        panel.setLayout(new BorderLayout(0, 0));
        panel.setSize(800, 600);

        JLabel titleLabel = new JLabel(titleName,JLabel.CENTER);
        titleLabel.setBorder(new EmptyBorder(10, 10, 25, 10));
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));

        //第一个Panel
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout(0, 0));
        panel.add(titlePanel, BorderLayout.NORTH);

        //添加标题信息
        titlePanel.add(titleLabel, BorderLayout.NORTH);

        // 时间戳转换为日期这个Panel
        JPanel jsonInputPanel = getInputPanel();
        titlePanel.add(jsonInputPanel, BorderLayout.CENTER);

        // 日期字符串转换为时间戳Panel
        JPanel buttonPanel = getButtonPanel();
        titlePanel.add(buttonPanel, BorderLayout.SOUTH);

        //第二个Panel
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout(0, 0));
        panel.add(centerPanel, BorderLayout.CENTER);

        tipArea = new ScrollPanel();
        tipArea.setPreferredSize(new Dimension(200, 350));
        centerPanel.add(tipArea, BorderLayout.CENTER);
        return panel;
    }

    private static JPanel getInputPanel() {
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_2.setBorder(new TitledBorder(UIManager
                .getBorder("TitledBorder.border"), "源json字符串",
                TitledBorder.LEADING, TitledBorder.TOP, null,
                new Color(0, 0, 0)));

        inputArea = new ScrollPanel();
        inputArea.setText("");
        inputArea.setFont(new Font("宋体", Font.BOLD, 24));
        inputArea.setPreferredSize(new Dimension(1000,200));
        panel_2.add(inputArea);

        return panel_2;
    }

    /**
     * 获取目录的Panel
     * @return
     */
    public static JPanel getButtonPanel(){
        JPanel panel_1 = new JPanel();

        JButton button_1 = new JButton("格式化");
        button_1.setPreferredSize(new Dimension(100, 40));
        panel_1.add(button_1);

        //设置button监控
        button_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputJson = inputArea.getText();
                formatJson(inputJson,tipArea);
            }
        });
        return panel_1;
    }

    /**
     * 格式化json字符串
     * @param inputJson
     * @param tipArea
     */
    private static void formatJson(String inputJson, ScrollPanel tipArea) {
        String formatJson = JsonUtils.formatJson(inputJson);
        tipArea.setText(tipArea.getText() + formatJson);
    }
}
